// MIT © 2017 azu
export default class BugReporter {
    constructor(issueURL) {
        this.issueURL = issueURL;
        this.title = '';
        this.body = '';
        this.labels = 'BugReport';
    }

    getSelectedText() {
        if (window.getSelection) {
            const selection = window.getSelection();
            return selection ? selection.toString() : '';
        } else if (document.selection && document.selection.type === 'Text') {
            return document.selection.createRange().text;
        }
        return '';
    }

    setTitle(title) {
        this.title = title || '';
    }

    setBody(body) {
        this.body = body || '';
    }

    report() {
        const params = new URLSearchParams({
            title: this.title,
            body: this.body,
            labels: this.labels
        });
        
        const url = `${this.issueURL}?${params.toString()}`;
        window.open(url, '_blank');
    }
}